//
//  SMACMusicPlayManager.swift
//  SmiraAiChat
//
//  Created by mumu on 2024/9/23.
//

import UIKit
import AVFAudio


class SMACMusicPlayManager: NSObject {
   
    
    private var bgPlayerSMAC: AVAudioPlayer?
    
    static let sharedSMAC = SMACMusicPlayManager.init()
    
    override init() {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
        let session = AVAudioSession.sharedInstance()
        try? session.setCategory(.playAndRecord, mode: .default, options: AVAudioSession.CategoryOptions.defaultToSpeaker)
        
        try? session.setActive(true, options: AVAudioSession.SetActiveOptions.notifyOthersOnDeactivation)
      
    }
   
    //MARK: -  停止播放铃声
     func stopBgPlayerSMAC()  {
         guard let player = self.bgPlayerSMAC else {
             return
         }
         if player.isPlaying {
             player.stop()
        }
    }
    
    //MARK: -  播放铃声
    func startbgPlayerSMAC(musicStyle:MusicStyle) {
        stopBgPlayerSMAC()
        let localUrl = URL.init(fileURLWithPath: Bundle.main.path(forResource:musicStyle.rawValue + ".mp3", ofType: nil)!)
        
        bgPlayerSMAC = try? AVAudioPlayer.init(contentsOf: localUrl)
       
        bgPlayerSMAC?.numberOfLoops = -1
        bgPlayerSMAC?.prepareToPlay()
        
        guard let player = self.bgPlayerSMAC else {
            return
        }
        player.play()
    }
}
